<?php
namespace Home\Controller;
use Think\Controller;
class FriendsController extends HController {
    public function index() {
        $user_model = M('User');
        $friends_model = M('Friends');
        $where1 = array('uid'=>$_SESSION['qzone']['home']['id'],'state'=>1);
        $friendslist = $friends_model->where($where1)->select();
        foreach ($friendslist as $k => $v) {
            $fuser = $user_model->find($v['who_id']);
            $friendslist[$k]['fuser'] = $fuser;
        }
        $homeuser = $user_model->find($_SESSION['qzone']['home']['id']);//查询当前用户信息
        $this->assign('user',$homeuser);// 赋值当前用户信息输出
        $this->assign('vuser',$homeuser);// 赋值当前用户信息输出
        $this->assign('friendslist',$friendslist);// 赋值当前用户好友列表信息输出
        $this->display();
    }

    public function search() {
        $user_model = M('User');
        $friends_model = M('Friends');
        $name = I('post.name');
        $list = array();
        if(!empty($name)){
            $map['username'] = array('like',"%$name%");
            $list = $user_model->where($map)->select();
        }
        $homeuser = $user_model->find($_SESSION['qzone']['home']['id']);//查询当前用户信息
        $this->assign('user',$homeuser);// 赋值当前用户信息输出
        $this->assign('vuser',$homeuser);// 赋值当前用户信息输出
        $this->assign('list',$list);// 赋值当前用户信息输出
        $this->display();
    }

    public function request() {
        $user_model = M('User');
        $friends_model = M('Friends');
        $where1 = array('who_id'=>$_SESSION['qzone']['home']['id']);
        $map['state'] = array('in','0,4');
        $requestlist = $friends_model->where($where1)->where($map)->select();
        foreach ($requestlist as $k => $v) {
            $fuser = $user_model->find($v['uid']);
            $requestlist[$k]['fuser'] = $fuser;
        }

        $homeuser = $user_model->find($_SESSION['qzone']['home']['id']);//查询当前用户信息
        $this->assign('user',$homeuser);// 赋值当前用户信息输出
        $this->assign('vuser',$homeuser);// 赋值当前用户信息输出
        $this->assign('requestlist',$requestlist);// 赋值当前用户信息输出
        $this->display();
    }


    //添加好友，发送好友请求
    public function add() {
        if(IS_AJAX){
            $data1['uid'] = I('post.id');
            $data1['who_id'] = $_SESSION['qzone']['home']['id'];
            $data1['state'] = 0;
            $friends_model = M('Friends');
            $rs1 = $friends_model->where($data1)->find();
            // dump($rs1);
            // echo $friends_model->getLastSql();exit;
            if($rs1){
                $state['state'] = 1;
                $friends_model->where(array('id'=>$rs1['id']))->save($state);
                $data2['who_id'] = I('post.id');
                $data2['uid'] = $_SESSION['qzone']['home']['id'];
                $data2['state'] = 1;
                $data2['ctime'] = time();
                $friends_model->create();
                $friends_model->add($data2);
                $result['status'] = 'success';
                $result['msg'] = '添加好友成功！';
                $this->ajaxReturn($result);
                exit;
            }

            $data3['who_id'] = I('post.id');
            $data3['uid'] = $_SESSION['qzone']['home']['id'];
            // dump($data);
            $map['state'] = array('in','0,1');
            
            $rs = $friends_model->where($data3)->where($map)->find();
            

            if($rs['state']==1){
                $result['status'] = 'failed';
                $result['msg'] = '你们已经是好友了！';
                $this->ajaxReturn($result);
                exit;
            }

            $data3['ctime'] = time();
            if($rs['state']==='0'){
                $res = $friends_model->where(array('id'=>$rs['id']))->save($data3);
            }else{
                $res = $friends_model->add($data3);
            }

            if($res){
                $result['status'] = 'success';
                $result['msg'] = '好友请求已发送！';
                $this->ajaxReturn($result);
            }else{
                $result['status'] = 'failed';
                $result['msg'] = '添加失败，请重试';
                $this->ajaxReturn($result);
            }
        }
    }

    //同意好友申请
    public function agree() {
        if(IS_AJAX){
            $id = I('post.id');
            $data['state'] = 1;
            $data['ctime'] = time();
            $friends_model = M('Friends');
            $res1 = $friends_model->where(array('id'=>$id))->save($data);
            $data['who_id'] = I('post.fid');
            $data['uid'] = $_SESSION['qzone']['home']['id'];
            $res2 = $friends_model->add($data);
            if($res1 && $res2){
                $rs['status'] = 'success';
                $rs['msg'] = '已同意';
                $this->ajaxReturn($rs);
            }else{
                $rs['status'] = 'failed';
                $rs['msg'] = '添加失败，请重试';
                $this->ajaxReturn($rs);
            }
        }
    }

    //拒绝好友申请
    public function refuse() {
        if(IS_AJAX){
            $where['id'] = I('post.id');
            $data['state'] = 4;//表示请求被拒绝
            $friends_model = M('Friends');
            $res = $friends_model->where($where)->save($data);
            if($res){
                $rs['status'] = 'success';
                $rs['msg'] = '已拒绝';
                $this->ajaxReturn($rs);
            }else{
                $rs['status'] = 'failed';
                $rs['msg'] = '操作失败，请重试';
                $this->ajaxReturn($rs);
            }
        }
    }

    //删除好友
    public function del() {
        if(IS_AJAX){
            $id= I('post.id');
            $data2['uid'] = I('post.fid');
            $data2['who_id'] = $_SESSION['qzone']['home']['id'];
            $data2['state'] = 1;
            $friends_model = M('Friends');
            $res1 = $friends_model->delete($id);
            $res2 = $friends_model->where($data2)->delete();
            // echo $friends_model->getLastSql();exit;
            if($res1&&$res2){
                $rs['status'] = 'success';
                $rs['msg'] = '删除成功';
                $this->ajaxReturn($rs);
            }else{
                $rs['status'] = 'failed';
                $rs['msg'] = '删除失败，请重试';
                $this->ajaxReturn($rs);
            }
        }
    }
    
}